package com.sumainfor.pc.market.service.tenant.impl;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.pc.market.dao.HousingMapper;
import com.sumainfor.pc.market.dao.OwnerMapper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * 租客端业主
 * @Auther: zhlu
 * @Date: 2019/3/12 19:20
 * @Description:
 */
@Service
public class TenantOwnerService {

    Logger log= LoggerFactory.getLogger(TenantOwnerService.class);

    @Autowired
    OwnerMapper OwnerMapper;

    @Autowired
    HousingMapper HousingMapper;

    /**
     * 功能描述: 
     * 根据微信端的用户手机号判断当前租客是否是业主
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/12 19:22
     */
    public Map<String,Object> getSigningWechatOwner(Map<String,Object>params){
        Map<String,Object> getSigningWechatOwner=OwnerMapper.getSigningWechatOwner(params);
        return getSigningWechatOwner;
    }

    /**
     * 功能描述: 
     * 根据openid获取当前业主托管房源总数
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/13 14:01
     */
    public Integer getWechatOwnerCount(Map<String,Object>params){
        Integer getWechatOwnerCount=OwnerMapper.getWechatOwnerCount(params);
        return getWechatOwnerCount;
    }

    /**
     * 功能描述: 
     * 根据业主手机号获取业主房源情况
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/13 19:40
     */
    public List<Map<String,Object>>getWechatOwnerHousing(Map<String,Object>params){
        List<Map<String,Object>>getWechatOwnerHousing=HousingMapper.getWechatOwnerHousing(params);
        if(getWechatOwnerHousing.size()>0){
            for(Map<String,Object> map:getWechatOwnerHousing){
                // 估计房源主编号获取当前房源状态
                Map<String,Object>getWechatOwnerHousingStatus=HousingMapper.getWechatOwnerHousingStatus(map);

                log.info("--------------"+getWechatOwnerHousingStatus.get("houstatus"));
                if(getWechatOwnerHousingStatus.get("houstatus").equals("0")
                        ||getWechatOwnerHousingStatus.get("houstatus").toString()=="0"){
                    map.put("houinfoStatus","0");
                }else{
                    map.put("houinfoStatus",getWechatOwnerHousingStatus.get("houinfoStatus"));
                }

                if(!StringUtils.isBlank(MapUtils.getString(map,"ProLoculus"))){
                    if(!StringUtils.isBlank(MapUtils.getString(map,"ProHall"))){
                        if(!StringUtils.isBlank(MapUtils.getString(map,"ProGuard"))){
                            map.put("house",map.get("ProLoculus").toString()+"室"+map.get("ProHall").toString()+"厅"+map.get("ProGuard").toString()+"卫");
                        }else{
                            map.put("house",map.get("ProLoculus").toString()+"室"+map.get("ProHall").toString()+"厅");
                        }
                    }else{
                        map.put("house",map.get("ProLoculus").toString()+"室");
                    }
                }else{
                    map.put("house",null);
                }
            }
        }
        return getWechatOwnerHousing;
    }

    /**
     * 功能描述: 
     * 根据业主手机号获取业主房源情况总数
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/31 11:43
     */
    public Integer getWechatOwnerHousingCount(Map<String,Object>params){
        Integer getWechatOwnerHousingCount=HousingMapper.getWechatOwnerHousingCount(params);
        return getWechatOwnerHousingCount;
    }

    /**
     * 功能描述: 
     * 根据业主房源主编号获取房源信息
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/13 20:58
     */
    public Map<String,Object>getWechatHousingMap(Map<String,Object>params)throws Exception{
        Map<String,Object>getWechatHousingMap=HousingMapper.getWechatHousingMap(params);
        if(!StringUtils.isBlank(MapUtils.getString(getWechatHousingMap,"ProLoculus"))){
            if(!StringUtils.isBlank(MapUtils.getString(getWechatHousingMap,"ProHall"))){
                if(!StringUtils.isBlank(MapUtils.getString(getWechatHousingMap,"ProGuard"))){
                    getWechatHousingMap.put("house",getWechatHousingMap.get("ProLoculus").toString()+"室"+getWechatHousingMap.get("ProHall").toString()+"厅"+getWechatHousingMap.get("ProGuard").toString()+"卫");
                }else{
                    getWechatHousingMap.put("house",getWechatHousingMap.get("ProLoculus").toString()+"室"+getWechatHousingMap.get("ProHall").toString()+"厅");
                }
            }else{
                getWechatHousingMap.put("house",getWechatHousingMap.get("ProLoculus").toString()+"室");
            }
        }else{
            getWechatHousingMap.put("house",null);
        }

        List<Map<String,Object>>getWechatHousingInfoList=HousingMapper.getWechatHousingInfoList(params);
        if(getWechatHousingInfoList.size()>0){
            for(Map<String,Object> map:getWechatHousingInfoList){
                //当前房源已签约，空置天数为0
                if(map.get("State").equals(2)||map.get("State").toString()=="2"){
                    map.put("Vacant",0);
                }else{//其他状态计算空置天数
                    int Vacant=0;
                    //如果最后一次退租时间不为空就拿最后一次退租计算
                    if(FieldCheckUtil.isMapFieldNotNull(params,new String[]{"LastTime"})){
                        //根据获取最后一次退租时间计算空置天数
                        Vacant= ConvertDateTime.subDate(ConvertDateTime.dateFormatStrToTime(ConvertDateTime.FORMAT_FULL_DATETIME_EN,map.get("LastTime").toString()),ConvertDateTime.dateFormatStrToTime(ConvertDateTime.FORMAT_FULL_DATETIME_EN,ConvertDateTime.getCurrentTime()),3);
                    }else{
                        //如果最后一次退租时间为空，那就拿验房时间计算空置天数
                        Vacant= ConvertDateTime.subDate(ConvertDateTime.dateFormatStrToTime(ConvertDateTime.FORMAT_FULL_DATETIME_EN,map.get("Accepted").toString()),ConvertDateTime.dateFormatStrToTime(ConvertDateTime.FORMAT_FULL_DATETIME_EN,ConvertDateTime.getCurrentTime()),3);
                    }
                    map.put("Vacant",Vacant);
                }
            }
        }
        getWechatHousingMap.put("HousingInfoList",getWechatHousingInfoList);
        return getWechatHousingMap;
    }

    /**
     * 功能描述: 
     * 根据业主账单编号获取业主银行卡资料、业主账单资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/1 19:51
     */
    public Map<String,Object>getOwnerDealPayMap(Map<String,Object>params){
        Map<String,Object>getOwnerDealPayMap=OwnerMapper.getOwnerDealPayMap(params);
        return getOwnerDealPayMap;
    }
}
